package com.ruoyi.web.controller.business.base;

import com.ruoyi.business.base.domain.BaseSecondary;
import com.ruoyi.business.base.service.IBaseSecondaryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地区公司信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/base/secondary")
public class BaseSecondaryController extends BaseController
{
    @Autowired
    private IBaseSecondaryService secondaryService;

    /**
     * 获取地区公司列表
     */
    @PreAuthorize("@ss.hasPermi('base:secondary:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseSecondary secondary)
    {
        startPage();
        List<BaseSecondary> list = secondaryService.selectSecondaryList(secondary);
        return getDataTable(list);
    }
    
    @Log(title = "地区公司管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('base:secondary:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseSecondary secondary)
    {
        List<BaseSecondary> list = secondaryService.selectSecondaryList(secondary);
        ExcelUtil<BaseSecondary> util = new ExcelUtil<BaseSecondary>(BaseSecondary.class);
        util.exportExcel(response, list, "地区公司数据");
    }

    /**
     * 根据地区公司编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:secondary:query')")
    @GetMapping(value = "/{secondaryId}")
    public AjaxResult getInfo(@PathVariable Long secondaryId)
    {
        return success(secondaryService.selectSecondaryById(secondaryId));
    }

    /**
     * 新增地区公司
     */
    @PreAuthorize("@ss.hasPermi('base:secondary:add')")
    @Log(title = "地区公司管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BaseSecondary secondary)
    {
        if (!secondaryService.checkSecondaryNameUnique(secondary))
        {
            return error("新增地区公司'" + secondary.getSecondaryName() + "'失败，地区公司名称已存在");
        }
        else if (!secondaryService.checkSecondaryCodeUnique(secondary))
        {
            return error("新增地区公司'" + secondary.getSecondaryName() + "'失败，地区公司编码已存在");
        }
        secondary.setCreateBy(getUsername());
        return toAjax(secondaryService.insertSecondary(secondary));
    }

    /**
     * 修改地区公司
     */
    @PreAuthorize("@ss.hasPermi('base:secondary:edit')")
    @Log(title = "地区公司管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BaseSecondary secondary)
    {
        if (!secondaryService.checkSecondaryNameUnique(secondary))
        {
            return error("修改地区公司'" + secondary.getSecondaryName() + "'失败，地区公司名称已存在");
        }
        else if (!secondaryService.checkSecondaryCodeUnique(secondary))
        {
            return error("修改地区公司'" + secondary.getSecondaryName() + "'失败，地区公司编码已存在");
        }
        secondary.setUpdateBy(getUsername());
        return toAjax(secondaryService.updateSecondary(secondary));
    }

    /**
     * 删除地区公司
     */
    @PreAuthorize("@ss.hasPermi('base:secondary:remove')")
    @Log(title = "地区公司管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{secondaryIds}")
    public AjaxResult remove(@PathVariable Long[] secondaryIds)
    {
        return toAjax(secondaryService.deleteSecondaryByIds(secondaryIds));
    }

    /**
     * 获取地区公司选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<BaseSecondary> secondarys = secondaryService.selectSecondaryAll();
        return success(secondarys);
    }
}
